package com.yuchting.dinneronline.client.common;

import com.google.gwt.dom.client.Style.Cursor;
import com.google.gwt.event.dom.client.LoadEvent;
import com.google.gwt.event.dom.client.LoadHandler;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseMoveEvent;
import com.google.gwt.event.dom.client.MouseMoveHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;

public class RankPanel extends HorizontalPanel {

	private final Image[] m_imageArray = new Image[5];
	
	private final String	m_starEmptyUrl;
	private final String	m_starMarkUrl;
	
	private boolean m_disabled = false;
	
	private int	m_imageSize = 0;
	private int m_rank = 0;
	
	/**
	 * generate a rank panel to click for rank 
	 * 
	 * @param _starEmptyUrl		rank star image URL path for empty state
	 * @param _starMarkUrl		rank star image URL path for marked state
	 */
	public RankPanel(String _starEmptyUrl,String _starMarkUrl){
		super();
		m_starEmptyUrl	= _starEmptyUrl;
		m_starMarkUrl	= _starMarkUrl;
		
		setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		
		for(int i = 0;i < m_imageArray.length;i++){
			m_imageArray[i] = new Image(m_starEmptyUrl);
			add(m_imageArray[i]);
		}
		
		m_imageArray[0].addLoadHandler(new LoadHandler() {
			
			@Override
			public void onLoad(LoadEvent event) {
				m_imageSize = m_imageArray[0].getWidth();				
			}
		});
		
		addDomHandler(new MouseMoveHandler() {
			
			@Override
			public void onMouseMove(MouseMoveEvent event) {
				processMouseEvent(event.getX(),event.getX(),false);				
			}
		},MouseMoveEvent.getType());
		
		addDomHandler(new MouseDownHandler() {
			
			@Override
			public void onMouseDown(MouseDownEvent event) {
				processMouseEvent(event.getX(),event.getX(),true);				
			}
		},MouseDownEvent.getType());
		
		addDomHandler(new MouseOutHandler() {
			
			@Override
			public void onMouseOut(MouseOutEvent event) {
				displayMarked(m_rank);
				
			}
		},MouseOutEvent.getType());
		
		getElement().getStyle().setCursor(Cursor.POINTER);
	}
	
	/**
	 * get the control panel rank [0-5]
	 * @return store rank
	 */
	public int getRank(){return m_rank;}
	
	/**
	 * set the control panel rank [0-5]
	 */
	public void setRank(int _rank)throws IllegalArgumentException{
		if(_rank < 0 || _rank > 5){
			throw new IllegalArgumentException("Must set the rank [0-5]");
		}
		
		m_rank = _rank;
		displayMarked(_rank);
	}
	
	/**
	 * set the enable of thes 
	 * @param _enable
	 */
	public void setEnable(boolean _enable){
		m_disabled = !_enable;
		
		if(m_disabled){
			getElement().getStyle().setCursor(Cursor.DEFAULT);
		}else{
			getElement().getStyle().setCursor(Cursor.POINTER);
		}
	}
	
	// mouse move
	private int m_bufferMoveRank = 0;
	private void processMouseEvent(int x ,int y,boolean _click){
		
		if(m_imageSize == 0 || m_disabled){
			return ;
		}
		
		int t_moveRank = x / m_imageSize + 1;
		if(m_bufferMoveRank == t_moveRank && !_click){
			return;
		}
		
		m_bufferMoveRank = t_moveRank;
		displayMarked(m_bufferMoveRank);
		
		if(_click){
			m_rank = m_bufferMoveRank;
		}
	}
	
	private void displayMarked(int _rank){
		
		for(int i = 0 ;i < m_imageArray.length;i++){
			if(i < _rank){
				m_imageArray[i].setUrl(m_starMarkUrl);
			}else{
				m_imageArray[i].setUrl(m_starEmptyUrl);
			}
		}
	}
}
